package tmproject.hlhj.fhp.anwser.customview

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator
import tmproject.hlhj.fhp.anwser.R
import java.util.logging.Handler

/**
 * Created by Never Fear   on 2018\8\20 0020.
Never More....
 */
class CustomTimeDown:View {
    private var mSec=60
    private var mPaint:Paint?=null
    private var textSize:Float?=20f
    private var oRadius:Float?=100f
    private var offsetRadius:Float?=10f
    private var progressWidth:Float?=20f
    private var msSec=1
    private var isGoing=true
    private var spAngle=0f
     var animator: ValueAnimator?=null

    var listener:TimeOverListener?=null

    @JvmOverloads
    constructor(context: Context?, attrs: AttributeSet?=null, defStyleAttr: Int=0) : super(context, attrs, defStyleAttr){
        val styledAttributes = context?.obtainStyledAttributes(attrs, R.styleable.CustomTimeDown)
        oRadius=styledAttributes?.getDimension(R.styleable.CustomTimeDown_oRadius,100f)
        offsetRadius=styledAttributes?.getDimension(R.styleable.CustomTimeDown_offsetRadius,20f)
        textSize=styledAttributes?.getDimension(R.styleable.CustomTimeDown_textSize,35f)
        mSec=styledAttributes?.getInt(R.styleable.CustomTimeDown_offSec,60)!!
        progressWidth=styledAttributes?.getDimension(R.styleable.CustomTimeDown_progressWidth,20f)
    }
    init {
        mPaint= Paint()
        mPaint?.let {
            it.isAntiAlias=true
        }

    }
    /*c测量高度*/
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)

        var width=0
        var height=0
        if (widthMode==MeasureSpec.AT_MOST){
            width=oRadius!!.toInt()
        }else{
            width=oRadius?.toInt()!!
        }
        if (heightMode==MeasureSpec.AT_MOST){
            height=oRadius!!.toInt()
        }else{
            height=oRadius!!.toInt()
        }
        setMeasuredDimension(width,height)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        /*画白色间隔*/
        mPaint?.color=Color.WHITE

        val oRectf = RectF(0f, 0f, oRadius!!, oRadius!!)
        canvas?.drawArc(oRectf,-90f,360f,true,mPaint)

//        滑进度条0xFFffe829.toInt(),
        val colors= intArrayOf(0xFFfaa124.toInt(),0xFFf5561f.toInt(),0xFFfaa124.toInt(),0xFFffe829.toInt(),0xFFfaa124.toInt())
        val pRectf = RectF(0f + offsetRadius!!, 0f + offsetRadius!!, oRadius!! - offsetRadius!!, oRadius!! - offsetRadius!!)
        val positions= floatArrayOf(-0.25f,0.25f,0.75f,1f)
        val gradient = SweepGradient(oRadius!! / 2, oRadius!! / 2, colors, null)
        mPaint?.color=Color.parseColor("#c3c3c3")
        canvas?.drawArc(pRectf,-90f,360f,true,mPaint)
        mPaint?.shader=gradient
//        canvas?.drawArc(pRectf,-90f,(msSec.toFloat()/mSec.toFloat())*360f,true,mPaint)
        canvas?.drawArc(pRectf,-90f,spAngle,true,mPaint)

        /*画阴影*/
        val mkRectf = RectF(0f + offsetRadius!! + progressWidth!!, 0f + offsetRadius!! + progressWidth!!, oRadius!! - offsetRadius!! - progressWidth!!, oRadius!! - offsetRadius!! - progressWidth!!)
        mPaint?.color=Color.GRAY
//        mPaint?.setShadowLayer(100f, 0f, 0f, Color.BLACK);
        mPaint?.shader=null
        canvas?.drawArc(mkRectf,-90f,360f,true,mPaint)
        /*内部白色*/
        val inRectf = RectF(0f + offsetRadius!! + progressWidth!!+2f, 0f + offsetRadius!! + progressWidth!!+2f, oRadius!! - offsetRadius!! - progressWidth!!-2f, oRadius!! - offsetRadius!! - progressWidth!!-2f)
        mPaint?.color=Color.WHITE
//        mPaint?.setShadowLayer(100f, 0f, 0f, Color.BLACK);
        mPaint?.shader=null
        canvas?.drawArc(inRectf,-90f,-360f,true,mPaint)

        /*字体*/
        mPaint?.color=Color.parseColor("#F55E1F")
        mPaint?.textSize=textSize!!
//        mPaint?.setShadowLayer(10f, 0f, 0f, Color.WHITE);

        mPaint?.typeface= Typeface.DEFAULT_BOLD
        canvas?.drawText(msSec.toString()+"s",oRadius!!/2-(textSize!!*msSec.toString().length*3/10),oRadius!!/2+(textSize!!/2),mPaint)
    }

    private val handler= @SuppressLint("HandlerLeak")
    object :android.os.Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            if (isGoing){
                if (msSec==mSec-1){
                    isGoing=false
                }
                msSec++
//                invalidate()
                this.sendEmptyMessageDelayed(1,1000)
            }
        }
    }
    fun startCountDown(){
    }
    fun stop(){
        animator?.let {
            it.cancel()
        }
        handler.removeMessages(1)
    }
    fun start(){
        handler?.sendEmptyMessageDelayed(1,0)
        if (animator==null){
            animator = ObjectAnimator.ofFloat( 0f, 360f)
            animator?.let{
                it.duration=mSec*1000.toLong()
                it.interpolator=LinearInterpolator()
                it.addUpdateListener(ValueAnimator.AnimatorUpdateListener {
                    spAngle= it.getAnimatedValue() as Float
                    invalidate()
                    if (spAngle==360f){
                        if (listener!=null){
                            listener?.timeOver()
                        }
                    }
                })
            }
        }
        animator?.start()
    }
    fun reStart(){
        if (animator!=null){
            Log.e("fhp","cancel不为空")
            animator?.cancel()
            spAngle=0f
            msSec=1
            animator=null
            handler?.removeMessages(1)
            isGoing=true
            start()
        }
    }
    override fun onDetachedFromWindow() {
        handler.removeMessages(1)
        animator?.let {
            it.cancel()
            animator=null
        }
        super.onDetachedFromWindow()
    }

    /*对外暴露接口*/
    interface TimeOverListener{
        fun timeOver()
    }
}